from informat import *
from maxpoints import *

def find_mahjong(cmd):
	try:
		s = parse_command_line(cmd)
	except:
		return {'is_mahjong':False}
	opts = get_options(s)
	if len(opts) == 0:
		return {'is_mahjong':False}
	max = 0
	ret = None
	for option in opts:
		fans = get_fans(option)
    		point_fans = make_one_fan_per_line(fans)
    		sets = option['sets']
    		exclusion_fans = add_exclusion_columns(point_fans, sets)
    		claimed_fans = optimal_claimed_fans(exclusion_fans)
    		score = score_of_claimed(exclusion_fans, claimed_fans)
		if score > max:
			max = score
			ret = make_full_result(score,claimed_fans,exclusion_fans,sets,option['w'])
	ret['is_mahjong'] = True
	return ret

def full_hand(hand_sets):
	ret = ''
	for aset in hand_sets:
		for tile in aset[1]:
			ret += tile
	return ret

def make_full_result(score,claimed_fans,exclusion_fans,hand_sets,wait_tile):
	print claimed_fans
	print exclusion_fans
	print hand_sets	
	ret = {'score':score, 'fans':[]}
	for i in claimed_fans:
		tile_string = ''
		points,name,sets,_,_,_,_ = exclusion_fans[i]
		if len(sets) == 1 and type(sets[0]) == str:
               		# A single tile (flower)
               		tile_string = sets[0]
            	elif len(sets) == 1 and type(sets[0]) == bool:
               		# A boolean like for single wait
               		tile_string = wait_tile
        	elif len(sets) >= 5:
        		tile_string = full_hand(hand_sets)
        	else:
        		tiles = []
        		for set_index in sets:
               			set_type, set_tiles = hand_sets[set_index]
				for ts in set_tiles:
               				tile_string += ts
		ret['fans'].append({'points':points, 'name':name, 'tiles':tile_string})
	return ret

def main():
    print 'coucouc'
    print find_mahjong('m b1b2b3 m b4b5b6 c b9b9b9b9 h b1b1b5b6 w b7 f F3F5F6 v b7b7b7')

if __name__ == '__main__':
    main()

